001 /*
002 * Copyright 2004 Niclas Hedhman.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
013 * implied.
014 *
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 package net.dpml.transit.monitor;
020
021 import java.lang.reflect.Constructor;
022 import java.net.URI;
023 import java.net.URL;
024 import java.net.URLClassLoader;
025
026 /**
027 * Adapts repository service monitor events to a logging channel.
028 *
029 * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
030 * @version 1.0.1
031 */
032 public class RepositoryMonitorAdapter extends AbstractAdapter
033 implements RepositoryMonitor
034 {
035 // ------------------------------------------------------------------------
036 // constructor
037 // ------------------------------------------------------------------------
038
039 /**
040 * Creation of a new repository monito to logging adapter.
041 * @param adapter the logging adapter
042 */
043 public RepositoryMonitorAdapter( Adapter adapter )
044 {
045 super( adapter );
046 }
047
048 // ------------------------------------------------------------------------
049 // RepositoryMonitor
050 // ------------------------------------------------------------------------
051
052 /**
053 * Handle notification of an information message.
054 * @param info the message
055 */
056 public void sequenceInfo( String info )
057 {
058 if( getAdapter().isDebugEnabled() )
059 {
060 getAdapter().debug( info );
061 }
062 }
063
064 /**
065 * Handle notification of a request for the establishment of a plugin.
066 * @param parent the parent classloader
067 * @param uri the requested plugin uri
068 * @param args the supplied constructor arguments
069 */
070 public void getPluginRequested( ClassLoader parent, URI uri, Object[] args )
071 {
072 if( getAdapter().isDebugEnabled() )
073 {
074 StringBuffer buffer = new StringBuffer();
075 buffer.append( "loading plugin " );
076 if( args.length == 1 )
077 {
078 buffer.append( " with a single argument" );
079 }
080 else if( args.length > 1 )
081 {
082 buffer.append( " with " + args.length );
083 buffer.append( " arguments" );
084 }
085 buffer.append( "\nuri: " + uri.toString() );
086 getAdapter().debug( buffer.toString() );
087 }
088 }
089
090 /**
091 * Handle notification of the establishment of a plugin class.
092 * @param clazz the plugin class
093 */
094 public void establishedPluginClass( Class clazz )
095 {
096 if( getAdapter().isDebugEnabled() )
097 {
098 StringBuffer buffer = new StringBuffer();
099 buffer.append( "loaded plugin class: " );
100 buffer.append( clazz.getName() );
101 getAdapter().debug( buffer.toString() );
102 }
103 }
104
105 /**
106 * Handle notification of an exception related to plugin establishment.
107 * @param method the method raising the exception
108 * @param e the causal exception
109 */
110 public void exceptionOccurred( String method, Exception e )
111 {
112 if( getAdapter().isErrorEnabled() )
113 {
114 String error = "Respostory error at : " + method;
115 getAdapter().error( error, e );
116 }
117 }
118
119 /**
120 * Handle notification of the discovery of a plugin constructor.
121 * @param constructor the constructor
122 * @param args the constructor args
123 */
124 public void pluginConstructorFound( Constructor constructor, Object[] args )
125 {
126 if( getAdapter().isDebugEnabled() )
127 {
128 Class[] classes = constructor.getParameterTypes();
129 if( classes.length == 0 )
130 {
131 getAdapter().debug( "plugin class contains a null constructor" );
132 }
133 else if( classes.length == 1 )
134 {
135 getAdapter().debug(
136 "plugin class contains a single constructor parameter" );
137 }
138 else
139 {
140 getAdapter().debug(
141 "plugin class contains "
142 + classes.length
143 + " constructor parameters" );
144 }
145 }
146 }
147
148 /**
149 * Handle notification of the instantiation of a plugin.
150 * @param plugin the plugin instance
151 */
152 public void pluginInstantiated( Object plugin )
153 {
154 if( getAdapter().isDebugEnabled() )
155 {
156 StringBuffer buffer = new StringBuffer();
157 buffer.append( "plugin: " );
158 buffer.append( plugin.getClass().getName() );
159 buffer.append( "#" );
160 buffer.append( System.identityHashCode( plugin ) );
161 getAdapter().debug( buffer.toString() );
162 }
163 }
164
165 /**
166 * Handle notification of the creation of a new classloader.
167 * @param type the type of classloader (api, spi or impl)
168 * @param classloader the new classloader
169 */
170 public void classloaderConstructed( String type, ClassLoader classloader )
171 {
172 if( getAdapter().isDebugEnabled() )
173 {
174 int id = System.identityHashCode( classloader );
175 StringBuffer buffer = new StringBuffer();
176 buffer.append( "created " );
177 buffer.append( type );
178 buffer.append( " classloader: " + id );
179 if( classloader instanceof URLClassLoader )
180 {
181 URLClassLoader loader = (URLClassLoader) classloader;
182 URL[] urls = loader.getURLs();
183 for( int i=0; i < urls.length; i++ )
184 {
185 URL url = urls[i];
186 buffer.append( "\n [" + i + "] \t" + url.toString() );
187 }
188 }
189 getAdapter().debug( buffer.toString() );
190 }
191 }
192
193 /**
194 * Handle notification of system classloader expansion.
195 * @param plugin the uri of the plugin requesting system classloader expansion
196 * @param urls the array of urls added to the system classloader
197 */
198 public void systemExpanded( URI plugin, URL[] urls )
199 {
200 if( getAdapter().isDebugEnabled() )
201 {
202 StringBuffer buffer = new StringBuffer();
203 buffer.append( "system classloader expansion" );
204 buffer.append( "\n plugin: " + plugin );
205 for( int i=0; i<urls.length; i++ )
206 {
207 int n = i+1;
208 buffer.append( "\n [" + n + "] \t" + urls[i] );
209 }
210 getAdapter().debug( buffer.toString() );
211 }
212 }
213 }